package com.tek.ding.faceding.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;

import androidx.fragment.app.FragmentActivity;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.tbruyelle.rxpermissions3.RxPermissions;
import com.tek.ding.faceding.R;
import com.tek.ding.faceding.bean.AppVersion;
import com.tek.ding.faceding.utils.update.UpdateService;

@SuppressLint("CheckResult")
public class DialogUtils {
    private static final String TAG = DialogUtils.class.getName();

    /**
     * app升级框
     *
     * @param context
     */
    public static void showUpdateDialog(Context context, AppVersion appVersion) {
        RxPermissions rxPermission = new RxPermissions((FragmentActivity) context);
        rxPermission.requestEachCombined(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(permission -> {
                    if (permission.granted) {
                        MaterialDialog.Builder builder = new MaterialDialog.Builder(context);
                        builder.title("下载最新版本 " + context.getResources().getString(R.string.fm_version, appVersion.getVersionName()))
                                .titleGravity(GravityEnum.CENTER)
                                .content(appVersion.getContent())
                                .contentColorRes(R.color.text_black)
                                .contentGravity(GravityEnum.START)
                                .positiveText("下载更新")
                                .onPositive(((dialog, which) -> {
                                    UpdateService.Builder
                                            .create(appVersion.getDownloadUrl())
                                            .setStoreDir("update")
                                            .setDownloadSuccessNotificationFlag(Notification.DEFAULT_ALL)
                                            .setDownloadErrorNotificationFlag(Notification.DEFAULT_ALL)
                                            .build(context);
                                }));
                        // 强制更新
                        if (appVersion.getForceStatus().equals(1))
                            builder.cancelable(false)
                                    .canceledOnTouchOutside(false);
                        else
                            builder.neutralText(R.string.cancel)
                                    .neutralColorRes(R.color.text_grey);
                        builder.show();
                    } else if (permission.shouldShowRequestPermissionRationale) {
                        // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时。还会提示请求权限的对话框
                        Log.d(TAG, permission.name + " is denied. More info should be provided.");
                    } else {
                        // 用户拒绝了该权限，而且选中『不再询问』
                        Log.d(TAG, permission.name + " is denied.");
                    }
                });
    }


    /**
     * 打开手机权限设置页面
     *
     * @param context
     */
    public static void openSystemPowerUi(Context context) {
        new MaterialDialog.Builder(context)
                .title("提示").titleGravity(GravityEnum.CENTER)
                .content("需要打开相关应用权限，请去设置中开启权限！").contentColorRes(R.color.text_black).contentGravity(GravityEnum.CENTER)
                .positiveText("打开设置")
                .neutralText(R.string.cancel)
                .onPositive(((dialog, which) -> {
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    //设置去向意图
                    Uri uri = Uri.fromParts("package", context.getPackageName(), null);
                    intent.setData(uri);
                    //发起跳转
                    context.startActivity(intent);
//                    BaseTools.openSystemPower(this);
                })).show();
    }
}
